<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../Vue全家桶_stu/03_依赖包/vue.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        table,
        tr,
        td {
            border: 1px solid #000;
            border-collapse: collapse;
        }

        #root {
            width: 600px;
            height: 400px;
            margin: 100px auto;
        }

        td {
            width: 150px;
            height: 50px;
            text-align: center;
        }

        button {
            width: 20px;
        }
    </style>
</head>

<body>
    <div id="root">
        <table>
            <tr v-for="item in goodsList" :key="item.id">
                <td>{{item.name}}</td>
                <td>{{item.price}}</td>
                <td>
                    <button @click="decrement(item)">-</button>
                    {{item.number}}
                    <button @click="increment(item)">+</button>
                </td>
                <td>{{item.price * item.number}}</td>
            </tr>
        </table>
        <h2>总计:{{totalPrice}}</h2>
    </div>
    <script>
        const vm = new Vue({
            el: "#root",
            data() {
                return {
                    goodsList: [
                        { name: '手机', price: 1999, number: 1, id: 1 },
                        { name: '电脑', price: 6999, number: 1, id: 2 },
                        { name: '手表', price: 999, number: 1, id: 3 },
                        { name: '外设', price: 798, number: 1, id: 4 },
                    ]
                }
            },
            methods: {
                increment(item) {
                    item.number++
                },
                decrement(item) {
                    item.number > 1 ? item.number-- : null
                }
            },
            computed: {
                //计算总价
                totalPrice() {
                    return this.goodsList.reduce((pre, cur) => { return pre + cur.number * cur.price }, 0)
                }
            }
        })
    </script>

</body>

</html>